package cz.muni.fi.pb138.videoweb2013.servlets;

import cz.muni.fi.pb138.videoweb2013.backend.Medium;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManager;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * update action
 * 
 * @author Filip
 */
@UrlBinding("/action/Update.jsp")
public class UpdateActionBean implements ActionBean{
    private ActionBeanContext context;
    private static int id;
    private static List<String> categoryHeader = new ArrayList<String>();
    private static List<String> categories = new ArrayList<String>();
    private static List<String> categoryAttributes = new ArrayList<String>();
    private static List<String> textFields = new ArrayList<String>();
    private String finished;

    
    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public List<String> getCategoryAttributes() {
        return categoryAttributes;
    }

    public void setCategoryAttributes(List<String> categoryAttributes) {
        this.categoryAttributes = categoryAttributes;
    }
    
    public int getId(){
        return id;
    }
    
    public void setId(int id){
        this.id = id;
    }

    public List<String> getCategoryHeader(){
        return categoryHeader;
    }
    
    public void setCategoryHeader(List<String> categoryHeader){
        this.categoryHeader = categoryHeader;
    }
    
    public List<String> getTextFields() { return textFields; }
    public void setTextFields(List<String> textFields) { this.textFields = textFields; }

    public String getFinished() {
        return finished;
    }

    public void setFinished(String finished) {
        this.finished = finished;
    }
    
    /**
     * print header and fills text fields with values of selected medium
     * 
     * @return 
     */
    @DefaultHandler
    public Resolution start() {
        categoryHeader.clear();
        categoryAttributes.clear();
        MediumManager mmi = new MediumManagerImpl();
        categoryHeader.addAll(mmi.getMediumById(id).getHeader());
        
        for (int i = 0; i < categoryHeader.size(); i++){
            categoryAttributes.add(mmi.getMediumById(id).getItem().get(categoryHeader.get(i)));
        }
        return new ForwardResolution("/Update.jsp");
    }
    
    /**
     * updates medium with new values selected by user
     * 
     * @return forward resolution to page Update
     */
    public Resolution update(){
        MediumManager mm = new MediumManagerImpl();
        Map<String,String> mpe = new HashMap<String,String>();
        for (int i = 0; i < textFields.size(); i++){
            mpe.put(categoryHeader.get(i), textFields.get(i));
        }
        mm.updateRecord(new Medium(id,mpe,mm.getMediumById(id).getCathegoryName(),mm.getMediumById(id).getCathegoryName().replaceAll("\\s",""),categoryHeader));
        finished = "Úspešne změneno";
        textFields.clear();
        return new ForwardResolution("/Update.jsp");
    }
    
}
